package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {
    @Autowired
    CourseTeacherMapper courseTeacherMapper;
    @Override
    public List<CourseTeacher> list(Long courseid) {
        LambdaQueryWrapper<CourseTeacher> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CourseTeacher::getCourseId,courseid);
        return courseTeacherMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public CourseTeacher save(CourseTeacher courseTeacher) {
        Long id = courseTeacher.getId();
        if (id==null){
            //id为空新增老师
            courseTeacher.setCreateDate(LocalDateTime.now());
            int insert = courseTeacherMapper.insert(courseTeacher);
            if (insert<1){
                XueChengPlusException.cast("新增失败");
            }
            return selectOne(courseTeacher);
        }
        if (id!=null){
            //修改老师信息
            int i = courseTeacherMapper.updateById(courseTeacher);
            if (i<1){
                XueChengPlusException.cast("修改失败");
            }

        }
        return selectOne(courseTeacher);
    }

    @Override
    public CourseTeacher selectOne(CourseTeacher courseTeacher) {
        return courseTeacherMapper.selectById(courseTeacher.getId());
    }

    @Override
    public void deleteOne(Long courseid, Long id) {
        courseTeacherMapper.deleteById(id);
    }
}
